//
//  ZZCNetworkTools.swift
//  weibo
//
//  Created by zhangzuchao on 17/3/29.
//  Copyright © 2017年 zhangZuChao. All rights reserved.
//

import UIKit
import AFNetworking

//请求类型的枚举
enum RequestType: Int{
    case get = 0
    case post = 1

}
//封装网络工具  获取数据回调
class ZZCNetworkTools: AFHTTPSessionManager {

    //网络工具单例全局访问点
    static let shareTools:ZZCNetworkTools = {
    
        //创建单例
        let tools = ZZCNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    
    
    /// 通用网络请求
    ///
    /// - Parameters:
    ///   - type: 请求类型
    ///   - urlStr: 请求地址
    ///   - params: 参数
    ///   - finishCallBack: 回调数据
    fileprivate func request(type: RequestType, urlStr: String,params: Any?,finishCallBack:@escaping (Any?, Error?)->()){
    
        if type == .post {
            
            post(urlStr, parameters: params, progress: nil, success: { (_, response) in
                
                finishCallBack(response, nil)
                
            }, failure: { (_, error) in
                
                finishCallBack(nil, error)
            })
        } else {
            
            get(urlStr, parameters: params, progress: nil, success: { (_, response) in
                
               finishCallBack(response, nil)
            }, failure: { (_, error) in
                
                finishCallBack(nil, error)
            })

        }
    
    
    }
    
}

//首页接口
extension ZZCNetworkTools{
    
    /// 发送带图片的微博内容
    ///
    /// - Parameters:
    ///   - accessToken: 令牌
    ///   - status: 内容
    ///   - pic: 图片
    ///   - finishCallBack: 数据回调
    func sendWeiboMessage(accessToken: String, status: String, pic: UIImage, finishCallBack: @escaping (Any?, Error?)->()) {
        //  请求地址
        let urlStr = "https://upload.api.weibo.com/2/statuses/upload.json"
        //  请求参数
        let params = [
            "access_token": accessToken,
            "status": status
        ]
        // compressionQuality 图片的质量系数
        let imageData = UIImageJPEGRepresentation(pic, 0.5)!

        post(urlStr, parameters: params, constructingBodyWith: { (formData) in
            formData.appendPart(withFileData: imageData, name: "pic", fileName: "test", mimeType: "image/jpeg")
        }, progress: nil, success: { (_, response) in
            finishCallBack(response, nil)
        }) { (_, error) in
            finishCallBack(nil, error)
        }
        
    }
    
    /// 发送文字微博
    ///
    /// - Parameters:
    ///   - accessToken: 令牌
    ///   - status: 文字内容
    ///   - finishCallBack: 数据回调
    func sendWeiboMessage(accessToken: String, status: String, finishCallBack: @escaping (Any?, Error?)->()) {
        //  请求地址
        let urlStr = "https://api.weibo.com/2/statuses/update.json"
        //  请求参数
        let params = [
            "access_token": accessToken,
            "status": status
        ]
        //  发起网络请求
        request(type: .post, urlStr: urlStr, params: params, finishCallBack: finishCallBack)
    }
    
    //获取首页文字
    func requestHomeData(accessToken: String, maxId: Int64 = 0, sinceId: Int64 = 0, finishCallBack: @escaping (Any?,Error?) -> ()) {
        
        let urlStr = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        let params:[String: Any] =
            [
                "access_token":accessToken,
                "max_id": maxId,
                "since_id": sinceId
           ]
       
        
//        print(urlStr + "?access_token=\(accessToken)")
        
        //双层回调
        request(type: .get, urlStr: urlStr, params: params, finishCallBack: finishCallBack)
        
    }
}

//OAuth登录接口
extension ZZCNetworkTools{

    func requestUserInfo(accessToken: String, uid:Int64,finishCallBack: @escaping (Any?,Error?) -> ()){
        
        let urlStr = "https://api.weibo.com/2/users/show.json"
        
        let params:[String: Any] = [
            "access_token":accessToken,
            "uid":uid
        ]
        
        //双层回调
        request(type: .get, urlStr: urlStr, params: params, finishCallBack: finishCallBack)

    }
}

//获取accessToken令牌 有令牌才能获得其他的网络数据
extension ZZCNetworkTools{
     func requestAccessToken(code: String, finishCallBack: @escaping (Any?, Error?)->()) {
        
        let urlStr = "https://api.weibo.com/oauth2/access_token"
        
        let params: [String: Any] = [
            "client_id": AppKey,
            "client_secret": AppSecret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": Redirect_Uri

        ]
        
        //双层回调
        request(type: .post, urlStr: urlStr, params: params, finishCallBack: finishCallBack)
        
    }
}


















